﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WHControlLib.Controls.LED
{
    //***********模拟单个 LED数码管*********************************************************
    /* 显示位置序号
      *  ****1***
      *  *      *  
      *  6      2
      *  *      *
      *  ****7***
      *  *      *
      *  5      3
      *  *      *
      *  ****4***
      */
    //**********显示的数字或字符用Value设置**********************
    //*****当不在字典支持的列表中的时候 val会被赋值未‘？’表示输入不支持或存在输入错误****当是！号时表示不显示本字符********
    [DefaultEvent("Click")]
    [DefaultProperty("Value")]
    [ToolboxItem(false)]
    public partial class baseLED : UserControl
    {
     

        public baseLED()
        {


            //设置双缓冲开启禁止背景擦拭
            SetStyle(
                  ControlStyles.OptimizedDoubleBuffer
                  | ControlStyles.ResizeRedraw
                  | ControlStyles.Selectable
                  | ControlStyles.AllPaintingInWmPaint
                  | ControlStyles.UserPaint
                  | ControlStyles.SupportsTransparentBackColor,
                  true);
            ////背景定义为透明色   
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;


            InitializeComponent();
            DictionaryIni();
        }
        void  DictionaryIni()
        {
            m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 };
            m_nums['1'] = new int[] { 2, 3 };
            m_nums['2'] = new int[] { 1, 2, 5, 4, 7 };
            m_nums['3'] = new int[] { 1, 2, 7, 3, 4 };
            m_nums['4'] = new int[] { 2, 3, 6, 7 };
            m_nums['5'] = new int[] { 1, 6, 7, 3, 4 };
            m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 };
            m_nums['7'] = new int[] { 1, 2, 3 };
            m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 };
            m_nums['A'] = new int[] { 1, 2, 3, 5, 6, 7 };
            m_nums['b'] = new int[] { 3, 4, 5, 6, 7 };
            m_nums['C'] = new int[] { 1, 6, 5, 4 };
            m_nums['c'] = new int[] { 7, 5, 4 };
            m_nums['d'] = new int[] { 2, 3, 4, 5, 7 };
            m_nums['E'] = new int[] { 1, 4, 5, 6, 7 };
            m_nums['F'] = new int[] { 1, 5, 6, 7 };
            m_nums['H'] = new int[] { 2, 3, 5, 6, 7 };
            m_nums['h'] = new int[] { 3, 5, 6, 7 };
            m_nums['J'] = new int[] { 2, 3, 4 };
            m_nums['L'] = new int[] { 4, 5, 6 };
            m_nums['o'] = new int[] { 3, 4, 5, 7 };
            m_nums['P'] = new int[] { 1, 2, 5, 6, 7 };
            m_nums['r'] = new int[] { 5, 7 };
            m_nums['U'] = new int[] { 2, 3, 4, 5, 6 };
            m_nums['-'] = new int[] { 7 };
            m_nums[':'] = new int[0];
            m_nums['.'] = new int[0];
            ////错误或不支持的字符的显示方式 三
            m_nums['?'] = new int[] { 1,7,4 };
              //当是！号时表示不显示本字符
            m_nums['!'] = new int[0];

        }

        //全局参数定义
        //public enum LEDNumChar : int
        //{
        //    /// <summary>
        //    /// The character 0
        //    /// </summary>
        //    CHAR_0 = (int)'0',
        //    /// <summary>
        //    /// The character 1
        //    /// </summary>
        //    CHAR_1 = (int)'1',
        //    /// <summary>
        //    /// The character 2
        //    /// </summary>
        //    CHAR_2 = (int)'2',
        //    /// <summary>
        //    /// The character 3
        //    /// </summary>
        //    CHAR_3 = (int)'3',
        //    /// <summary>
        //    /// The character 4
        //    /// </summary>
        //    CHAR_4 = (int)'4',
        //    CHAR_5 = (int)'5',
        //    /// <summary>
        //    /// The character 6
        //    /// </summary>
        //    CHAR_6 = (int)'6',
        //    /// <summary>
        //    /// The character 7
        //    /// </summary>
        //    CHAR_7 = (int)'7',
        //    /// <summary>
        //    /// The character 8
        //    /// </summary>
        //    CHAR_8 = (int)'8',
        //    /// <summary>
        //    /// The character 9
        //    /// </summary>
        //    CHAR_9 = (int)'9',
        //    /// <summary>
        //    /// The character a
        //    /// </summary>
        //    CHAR_A = (int)'A',
        //    /// <summary>
        //    /// The character b
        //    /// </summary>
        //    CHAR_b = (int)'b',
        //    /// <summary>
        //    /// The character c
        //    /// </summary>
        //    CHAR_C = (int)'C',
        //    /// <summary>
        //    /// The character c
        //    /// </summary>
        //    CHAR_c = (int)'c',
        //    /// <summary>
        //    /// The character d
        //    /// </summary>
        //    CHAR_d = (int)'d',
        //    /// <summary>
        //    /// The character e
        //    /// </summary>
        //    CHAR_E = (int)'E',
        //    /// <summary>
        //    /// The character f
        //    /// </summary>
        //    CHAR_F = (int)'F',
        //    /// <summary>
        //    /// The character h
        //    /// </summary>
        //    CHAR_H = (int)'H',
        //    /// <summary>
        //    /// The character h
        //    /// </summary>
        //    CHAR_h = (int)'h',
        //    /// <summary>
        //    /// The character j
        //    /// </summary>
        //    CHAR_J = (int)'J',
        //    /// <summary>
        //    /// The character l
        //    /// </summary>
        //    CHAR_L = (int)'L',
        //    /// <summary>
        //    /// The character o
        //    /// </summary>
        //    CHAR_o = (int)'o',
        //    /// <summary>
        //    /// The character p
        //    /// </summary>
        //    CHAR_P = (int)'P',
        //    /// <summary>
        //    /// The character r
        //    /// </summary>
        //    CHAR_r = (int)'r',
        //    /// <summary>
        //    /// The character u
        //    /// </summary>
        //    CHAR_U = (int)'U',
        //    /// <summary>
        //    /// The character horizontal line
        //    /// </summary>
        //    CHAR_HorizontalLine = (int)'-',
        //    /// <summary>
        //    /// The character colon
        //    /// </summary>
        //    CHAR_Colon = (int)':',
        //    /// <summary>
        //    /// The character dot
        //    /// </summary>
        //    CHAR_Dot = (int)'.',
        //}

        private static Dictionary<char, int[]> m_nums = new Dictionary<char, int[]>();

        Rectangle MyRect = new Rectangle();
        Rectangle DrawRect = new Rectangle();
        //public LEDNumChar ValueChar
        //{
        //    get
        //    {
        //        return (LEDNumChar)((int)_lEDValueChar);
        //    }
        //    set
        //    {
        //        Value = (char)value;
        //    }
        //}
       

        #region  公共  属性 定义


        private Char _lEDValueChar= '0';
        [Category("A我的"), Description(" LED对应要显示的字符，默认，0 "), Browsable(true)]
        public Char Value
        {
            get { return _lEDValueChar; }
            set {
                //以后需要加入字符是否合法检查
            {
                    try
                    {
                 if (!m_nums.ContainsKey(value))
                    {
                        _lEDValueChar = '?';
                        Refresh();
                            return;
                        }
                    if (_lEDValueChar != value)
                    {
                        _lEDValueChar = value;
                            Invalidate();
                        }
                    }
                    catch 
                    {
                        _lEDValueChar = '?';
                        return;

                    } 
             
                }
            }
        }
        private int _lEDLineWidth=10;
        [Category("A我的"), Description(" LED字符中每一段的宽度，默认，10 "), Browsable(true)]
        public int LEDLineWidth
        {
            get { return _lEDLineWidth; }
            set
            {
                if (value<1)
                {
                    _lEDLineWidth = 1;
                }
                else
                _lEDLineWidth = value; Invalidate(); }
        }

        private Color _lEDLineColor=Color.Black;
        [Category("A我的"), Description(" LED字符中每一段的颜色，默认，黑色 "), Browsable(true)]
        public Color LEDLineColor
        {
            get { return _lEDLineColor; }
            set { _lEDLineColor = value; Invalidate(); }
        }

        private bool _isShowBorder;
        [Category("A我的"), Description(" 是否显示外形的边框的描边，默认，false"), Browsable(true)]
        public bool IsShowBorder
        {
            get { return _isShowBorder; }
            set { _isShowBorder = value; Invalidate(); }
        }

        private int _borderWidth=2;
        [Category("A我的"), Description(" 描边外形的边框宽度，默认，2，如果启用不能太大，最好是偶数 不然不美观,"), Browsable(true)]
        public int BorderWidth
        {
            get { return _borderWidth; }
            set {
                if (value<0)
                {
                    _borderWidth = 0;
                }
                _borderWidth = value; Invalidate(); }
        }
        private Color _borderColor=Color.Blue;
        [Category("A我的"), Description(" 外形的边框的描边的颜色，默认，蓝色"), Browsable(true)]
        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; Invalidate(); }
        }




        #endregion

        //***********************方法****************************
       private void BiforePainIni()
        {
            MyRect = ClientRectangle;
            MyRect.X = MyRect.X + 2;
            MyRect.Y = MyRect.Y + 2;
            MyRect.Width = MyRect.Width - 4;
            MyRect.Height = MyRect.Height - 4;

            if (IsShowBorder)
            {
                DrawRect.X = MyRect.X + (int)BorderWidth/2 ;
                DrawRect.Y = MyRect.Y + (int)BorderWidth/2 ;
                DrawRect.Width = MyRect.Width - BorderWidth;
                DrawRect.Height = MyRect.Height - BorderWidth;


            }
            else
            {
                DrawRect = MyRect; 
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            BiforePainIni();

            Graphics Myg = e.Graphics;
            Myg.SmoothingMode = SmoothingMode.AntiAlias;
            Myg.CompositingQuality = CompositingQuality.HighQuality;
            Myg.InterpolationMode = InterpolationMode.HighQualityBicubic;
            try
            {
             DrawLEDChar(Myg, DrawRect);
            }
            catch 
            {
                //当错误时显示错误字符 自定义一个
                //ValueChar = LEDNumChar.CHAR_0;
                Value = '?';
             


               
            }

          
        }

            void DrawLEDChar(Graphics Myg ,Rectangle DrawRect )
              
        {

            int penwidth = (int)BorderWidth / 2;

            if (_lEDValueChar == '!')
            {
                return;
            }
            //、、当是两个点的时候
           
            if (_lEDValueChar == '.')
            {
                Rectangle r2 = new Rectangle(DrawRect.Left + (DrawRect.Width - _lEDLineWidth) / 2, DrawRect.Bottom - _lEDLineWidth * 2, _lEDLineWidth, _lEDLineWidth);
                Myg.FillRectangle(new SolidBrush(LEDLineColor), r2);
                if (IsShowBorder)
                {
                    Myg.DrawRectangle(new Pen(BorderColor, BorderWidth), r2);
                } 
              }
            else if (_lEDValueChar == ':')
            {
                Rectangle r1 = new Rectangle(DrawRect.Left + (DrawRect.Width - _lEDLineWidth) / 2, DrawRect.Top + (DrawRect.Height / 2 - _lEDLineWidth) / 2, _lEDLineWidth, _lEDLineWidth);
                Myg.FillRectangle(new SolidBrush(LEDLineColor), r1);
                Rectangle r2 = new Rectangle(DrawRect.Left + (DrawRect.Width - _lEDLineWidth) / 2, DrawRect.Top + (DrawRect.Height / 2 - _lEDLineWidth) / 2 + DrawRect.Height / 2, _lEDLineWidth, _lEDLineWidth);
                Myg.FillRectangle(new SolidBrush(LEDLineColor), r2);
                if (IsShowBorder)
                {
                    Myg.DrawRectangle(new Pen(BorderColor, BorderWidth), r1);
                    Myg.DrawRectangle(new Pen(BorderColor, BorderWidth), r2);
                }

            }
            //当是数字或字母的时候
            else
            {
                int[] vs = m_nums[_lEDValueChar];
                if (vs.Contains(1))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Left + 2, DrawRect.Top),
                    new Point(DrawRect.Right - 2, DrawRect.Top),
                    new Point(DrawRect.Right - _lEDLineWidth-2, DrawRect.Top+_lEDLineWidth),
                    new Point(DrawRect.Left + _lEDLineWidth+2, DrawRect.Top+_lEDLineWidth),
                    new Point(DrawRect.Left + 2, DrawRect.Top)
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }

                if (vs.Contains(2))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Right, DrawRect.Top),
                    new Point(DrawRect.Right, DrawRect.Top+(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Right-_lEDLineWidth/2, DrawRect.Top+(DrawRect.Height-_lEDLineWidth-4)/2+_lEDLineWidth/2),
                    new Point(DrawRect.Right-_lEDLineWidth, DrawRect.Top+(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Right-_lEDLineWidth, DrawRect.Top+_lEDLineWidth),
                    new Point(DrawRect.Right, DrawRect.Top)
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }

                if (vs.Contains(3))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Right, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Right, DrawRect.Bottom),
                    new Point(DrawRect.Right-_lEDLineWidth, DrawRect.Bottom-_lEDLineWidth),
                    new Point(DrawRect.Right-_lEDLineWidth, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Right-_lEDLineWidth/2, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2-_lEDLineWidth/2),
                    new Point(DrawRect.Right, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2),
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }

                if (vs.Contains(4))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Left + 2, DrawRect.Bottom),
                    new Point(DrawRect.Right - 2, DrawRect.Bottom),
                    new Point(DrawRect.Right - _lEDLineWidth-2, DrawRect.Bottom-_lEDLineWidth),
                    new Point(DrawRect.Left + _lEDLineWidth+2, DrawRect.Bottom-_lEDLineWidth),
                    new Point(DrawRect.Left + 2, DrawRect.Bottom)
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }

                if (vs.Contains(5))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Left, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Left, DrawRect.Bottom),
                    new Point(DrawRect.Left+_lEDLineWidth, DrawRect.Bottom-_lEDLineWidth),
                    new Point(DrawRect.Left+_lEDLineWidth, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Left+_lEDLineWidth/2, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2-_lEDLineWidth/2),
                    new Point(DrawRect.Left, DrawRect.Bottom-(DrawRect.Height-_lEDLineWidth-4)/2),
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }


                if (vs.Contains(6))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Left, DrawRect.Top),
                    new Point(DrawRect.Left, DrawRect.Top+(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Left+_lEDLineWidth/2, DrawRect.Top+(DrawRect.Height-_lEDLineWidth-4)/2+_lEDLineWidth/2),
                    new Point(DrawRect.Left+_lEDLineWidth, DrawRect.Top+(DrawRect.Height-_lEDLineWidth-4)/2),
                    new Point(DrawRect.Left+_lEDLineWidth, DrawRect.Top+_lEDLineWidth),
                    new Point(DrawRect.Left, DrawRect.Top)
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }

                if (vs.Contains(7))
                {
                    GraphicsPath path = new GraphicsPath();
                    path.AddLines(new Point[]
                {
                    new Point(DrawRect.Left+_lEDLineWidth/2, DrawRect.Height/2+1),
                    new Point(DrawRect.Left+_lEDLineWidth, DrawRect.Height/2-_lEDLineWidth/2+1),
                    new Point(DrawRect.Right-_lEDLineWidth, DrawRect.Height/2-_lEDLineWidth/2+1),
                    new Point(DrawRect.Right-_lEDLineWidth/2, DrawRect.Height/2+1),
                    new Point(DrawRect.Right-_lEDLineWidth, DrawRect.Height/2+_lEDLineWidth/2+1),
                    new Point(DrawRect.Left+_lEDLineWidth, DrawRect.Height/2+_lEDLineWidth/2+1),
                    new Point(DrawRect.Left+_lEDLineWidth/2, DrawRect.Height/2+1)
                });
                    path.CloseAllFigures();
                    Myg.FillPath(new SolidBrush(LEDLineColor), path);
                    if (IsShowBorder)
                    {
                        Myg.DrawPath(new Pen(BorderColor, BorderWidth), path);
                    }
                }



            }
        }







        ////////////////////////////////////////////////////////////
        ///
        public enum LED_Char
        {

        }





        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
}
